package com.aisile.shop.controller;

import com.aisile.entity.PageResult;
import com.aisile.entity.Result;
import com.aisile.pojo.TbGoods;
import com.aisile.pojogroup.Goods;
import com.aisile.sellergoods.service.GoodsService;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @标题 GoodsController
 * @作者 Mr.chen
 * @创建日期 2019/1/3 11:54
 * @版本号 1.0
 **/
@RestController
@RequestMapping("goods")
public class GoodsController {
    @Reference
    GoodsService goodsService;

    @RequestMapping("save")
    public Result save(@RequestBody Goods goods){
        String sellerId = SecurityContextHolder.getContext().getAuthentication().getName();
        try {
            if(!StringUtils.isEmpty(sellerId)){
                goods.getGoods().setSellerId(sellerId);
                goodsService.save(goods);
                return new Result(true,"保存成功");
            }
            return new Result(false,"请先登录商家端");
        }catch (Exception e){
            return new Result(false,"服务器出现异常");
        }
    }
    @RequestMapping("findPage")
    public PageResult findPage(int page, int rows, @RequestBody TbGoods tbGoods){
        String sellerId = SecurityContextHolder.getContext().getAuthentication().getName();
        tbGoods.setSellerId(sellerId);
        return goodsService.findPage(page,rows,tbGoods);
    }

    @RequestMapping("findOne")
    public Goods findOne(long id){
        return goodsService.findOne(id);
    }
}
